import java.util.Arrays;

public class Test {

    /*
     * 和为S的两个数字
     * */
    public static void main(String[] args) {
        int[] nums = {2,7,11,15,17,20,19};
        int target = 30;
        System.out.println(Arrays.toString(twoSum1(nums, target)));
    }

    //暴力搜索枚举：
    public static int[] twoSum1(int[] nums,int target){
        int[] ret = {};
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if(nums[i] + nums[j] == target){
                    return new int[]{nums[i],nums[j]};
                }
            }
        }
        return new int[]{0};
    }

    //优化：
    public static int[] twoSum(int[] nums,int target){
        int left = 0;
        int right = nums.length - 1;
        while(left < right){
            if(nums[left] + nums[right] < target){
                left++;
            }else if(nums[left] + nums[right] > target){
                right--;
            }else{
                return new int[]{nums[left],nums[right]};
            }
        }
        return null;
    }

}
